<?php


/*
* Created on 15/05/2006
*
* @author Rafael Guerrero
*/
class PlaceSetListHandler extends BaseHandler {

	/**
	 * @type array
	 */
	private $nodesByName;

	/**
	 * @type array
	 */
	private $fieldByName;
	
	/**
	* @type array
	*/
	private $variantByName;

	/**
	 * @type array
	 */
	private $fieldStateByName;
	
	/**
	 * @type array
	 */
	private $variantListByName;

	/**
	 * @type MPlaceSet
	 */
	private $currPlaceSet;

	// ----------------------------------------
	// Props
	// ----------------------------------------

	/**
	 * @type array
	 */
	private $placeSetList = array ();
	
	/**
	 * @type array
	 */
	private $departmentListByName= array ();

	// ----------------------------------------

	public function __construct($nodesByName = NULL, $fieldByName = NULL, $variantByName) {
		$this->nodesByName = & $nodesByName;
		$this->fieldByName = & $fieldByName;
		$this->variantByName = & $variantByName;
	}

	// ----------------------------------------
	// Handler Methods
	// ----------------------------------------

	private function placeSetBeg($parser, $tag, $attributes) {
		
		$this->currPlaceSet =  parent :: $session->create('MPlaceSet');
		$this->currPlaceSet->setName($attributes['name']);

		$this->placeSetList[] =  $this->currPlaceSet;

		$this->fieldStateByName = array ();

		foreach ($this->fieldByName as $key => $docField) {

			$docFieldState = parent :: $session->create('MDocfieldState');
			$docFieldState->setState(MDocfieldState :: $STATE_HIDDEN);
			$docFieldState->setType(MDocfieldState :: $TYPE_PLACE_SET);

			//$docField -> getDocFieldStateList(); . add(docFieldState);
			$docFieldState->setDocFieldRef($docField);

			//currPlaceSet . getDocFieldStateList() . add(docFieldState);
			$docFieldState->setPlaceSetRef($this->currPlaceSet);

			$this->fieldStateByName[$docField->getName()] = $docFieldState;
		}
		
		foreach ($this->variantByName as $key => $variant) {

			$this->variantListByName[$variant->name] =  $variant->name;
		}
		
		
	}

	private function placeStateBeg($parser, $tag, $attributes) {
		
		$placeState = parent :: $session->create('MPlaceState');
		$placeState->setTokens((integer) $attributes['tokens']);

		if (!isset ($this->nodesByName[$attributes['place']])) {
			throw new Exception("place node not found: " + attributes . getValue("place"));
		}
		$place = $this->nodesByName[$attributes['place']];
		//currPlaceSet . getPlaceStateList() . add(placeState);
		$placeState->setPlaceSetRef($this->currPlaceSet);

		//place . getPlaceStateList() . add(placeState);
		$placeState->setPlaceRef($place);
	}
	
	
	//************************************************************
	private function departmentBeg($parser, $tag, $attributes) {
		echo '<br>identificador en el handler',$attributes['dptoName'];
		//$this->currPlaceSet = & parent :: $session->create('MPlaceSet');
		//$this->currPlaceSet->setName($attributes['name']);
		
		$query = 'FROM MDepartamento AS r WHERE r.identificadorUnico = ?';

		$dpto = BaseHandler :: $session->find($query, $attributes['dptoName']);

		if ($dpto == NULL) {
			throw new Exception("department not found: " . $attributes['dptoName']);
		}
		
		
		$dptoVariety=& parent :: $session->create('MDptoVariety');
		
		$dptoVariety->dptoName=$attributes['dptoName'];
		
		
		$this->currPlaceSet->dptoVarietyList[]=$dptoVariety;
		$this->placeSetList[] = & $this->currPlaceSet;
		
		$this->departmentListByName[$this->currPlaceSet->name]=$dptoVariety->dptoName;
	}

	private function captionEnd($parser, $tag) {
		
		$this->currPlaceSet->setCaption($this->text);
	}

	private function visibleDocFieldEnd($parser, $tag) {
		
		$fieldList = explode(',', $this->text);

		for ($i = 0; $i < count($fieldList); $i++) {
			if (!isset ($this->fieldStateByName[$fieldList[$i]])) {
				throw new Exception("Field not found: " . $fieldList[$i]);
			}

			$docFieldState = & $this->fieldStateByName[$fieldList[$i]];

			$docFieldState->setState(MDocfieldState :: $STATE_VISIBLE);
		}
	}
	
	private function variantsEnd($parser, $tag) {
		
		$variantList = explode(',', $this->text);
		$deptName=$variantList[0];
		for ($i = 1; $i < count($variantList); $i++) {
			if (!isset ($this->variantListByName[$variantList[$i]])) {
				throw new Exception("Variant not found: " . $variantList[$i]);
			}
			foreach($this->variantByName as $variant){
				
				if($variant->name==$variantList[$i] && $this->departmentListByName[$this->currPlaceSet->name]==$deptName){
					foreach($this->currPlaceSet->dptoVarietyList as  &$dv){
						if($dv->dptoName==$deptName){
							$dv->variantList[]=$variant;
						}
						
					}
					
					
					
				}
			}
		}
		
	}

	// ----------------------------------------
	// BaseHandler Methods
	// ----------------------------------------

	public function startElement($parser, $tag, $attributes) {
		parent :: startElement($parser, $tag, $attributes);

		switch ($tag) {
			case 'placeSet' :
				$this->placeSetBeg($parser, $tag, $attributes);
				break;

			case 'placeState' :
				$this->placeStateBeg($parser, $tag, $attributes);
				break;
			case 'department' :
				$this->departmentBeg($parser, $tag, $attributes);
				break;
		}
	}

	public function endElement($parser, $tag) {
		parent :: endElement($parser, $tag);

		switch ($tag) {
			case 'caption' :
				$this->captionEnd($parser, $tag);
				break;

			case 'visibleDocField' :
				$this->visibleDocFieldEnd($parser, $tag);
				break;
			case 'variants' :
				$this->variantsEnd($parser, $tag);
				break;
		}
	}

	// ----------------------------------------
	// Props Methods
	// ----------------------------------------

	public function getPlaceSetList() {
		return $this->placeSetList;
	}
}
?>
